// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Collections.Generic;
using System.Data;
using Microsoft.Data.SqlClient;
using JetBrains.Annotations;

namespace SnapObjects.Data.SqlServer
{
    internal class SqlServerAdoDbProvider : AdoDbProvider
    {
        public SqlServerAdoDbProvider(DataContext dataContext) :
            base(dataContext)
        {
        }

        protected override IAdoDbResultSet<TModel> CreateResultSet<TModel>(
            IDbCommand command, IDataReader reader, IEnumerable<IAdoDbParameter> parameters)
        {
            var mapper = new SqlServerTypeMapper(_dataContext.ContextOptions);

            var visitor = new SqlServerAdoDbDataVisitor(
                (SqlCommand)command, (SqlDataReader)reader, parameters, _dataContext.ContextOptions);

            return new SqlServerAdoDbResultSet<TModel>(visitor, mapper);
        }

        protected override IAdoDbDataVisitor CreateDataVisitor(
            IDbCommand command, IDataReader reader, IEnumerable<IAdoDbParameter> parameters)
        {
            return new SqlServerAdoDbDataVisitor(
                (SqlCommand)command, (SqlDataReader)reader, parameters, _dataContext.ContextOptions);
        }

        protected override IAdoDbCommand CreateAdoDbCommand(
            [NotNull] string sqlText,
            IEnumerable<IAdoDbParameter> parameters,
            CommandType commandType = CommandType.Text,
            bool addExceptionToTransaction = true)
        {
            return new SqlServerAdoDbCommand(sqlText, parameters, addExceptionToTransaction, commandType);
        }
    }
}
